package com.itheima.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.itheima.bos.domain.system.Menu;
import com.itheima.bos.domain.system.User;
import com.itheima.bos.service.system.IMenuService;
import com.itheima.bos.web.action.common.BaseAction;

public class MenuAction extends BaseAction<Menu> {

	// 注入menuservice
	@Autowired
	private IMenuService menuService;

	@Action(value = "menu_list", results = { @Result(name = "success", type = "json") })
	public String list() {

		// 调用service查询数据
		List<Menu> menus = menuService.findAll();

		// 返回json格式数据
		pushPageDataToValueStack(menus);
		return SUCCESS;
	}

	@Action(value = "menu_save", results = { @Result(name = "success", type = "redirect", location = "/pages/system/menu.html") })
	public String save() {

		// 调用业务层保存数据
		menuService.save(model);

		return SUCCESS;
	}

	// 动态展示菜单
	@Action(value = "menu_showmenu", results = { @Result(name = "success", type = "json") })
	public String showMenu() {
		//获取subject
		Subject subject = SecurityUtils.getSubject();
		//获取当前用户
		User user = (User) subject.getPrincipal();
		List<Menu>menus=menuService.findByUser(user);
		pushPageDataToValueStack(menus);
		return SUCCESS;
	}
}
